package com.cinc.mdm.modules.processRoute.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRoute;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRouteClass;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRouteDetail;
import com.cinc.mdm.modules.processRoute.model.MdmProcessRouteDetailModel;
import com.cinc.mdm.modules.processRoute.service.IMdmProcessRouteClassService;
import com.cinc.mdm.modules.processRoute.service.IMdmProcessRouteDetailService;
import com.cinc.mdm.modules.processRoute.service.IMdmProcessRouteService;
import com.cinc.mdm.modules.processRoute.vo.MdmProcessRouteClassPage;
import com.cinc.mdm.modules.processRoute.vo.MdmProcessRouteDetailPage;
import com.cinc.mdm.modules.processRoute.vo.MdmProcessRoutePage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 工艺路线
 * @Author: jeecg-boot
 * @Date: 2022-07-21
 * @Version: V1.0
 */
@Api(tags = "工艺路线")
@RestController
@RequestMapping("/mdm/processRoute")
@Slf4j
public class MdmProcessRouteController extends JeecgController<MdmProcessRoute, IMdmProcessRouteService> {
    @Autowired
    private IMdmProcessRouteDetailService mdmProcessRouteDetailService;
    @Autowired
    private IMdmProcessRouteService mdmProcessRouteService;
    @Autowired
    private IMdmProcessRouteClassService mdmProcessRouteClassService;

    /**
     * 分页列表查询
     *
     * @param mdmProcessRoute
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "工艺路线-分页列表查询")
    @ApiOperation(value = "工艺路线-分页列表查询", notes = "工艺路线-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(MdmProcessRoute mdmProcessRoute,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<MdmProcessRoute> queryWrapper = QueryGenerator.initQueryWrapper(mdmProcessRoute, req.getParameterMap());
        Page<MdmProcessRoute> page = new Page<MdmProcessRoute>(pageNo, pageSize);
        IPage<MdmProcessRoute> pageList = mdmProcessRouteService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 明细分页查询
     *
     * @param detailPage
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "工艺路线-分页列表查询")
    @ApiOperation(value = "工艺路线-分页列表查询", notes = "工艺路线-分页列表查询")
    @GetMapping(value = "/detailPageList")
    public Result<?> queryDetailPageList(MdmProcessRouteDetailPage detailPage,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) {
        Page<MdmProcessRouteDetailModel> page = new Page<>(pageNo, pageSize);
        mdmProcessRouteService.detaiPage(page, detailPage);
        return Result.OK(page);
    }

    /**
     * 添加
     *
     * @param mdmProcessRoutePage
     * @return
     */
    @AutoLog(value = "工艺路线-添加")
    @ApiOperation(value = "工艺路线-添加", notes = "工艺路线-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MdmProcessRoutePage mdmProcessRoutePage) {
        MdmProcessRoute mdmProcessRoute = new MdmProcessRoute();
        BeanUtils.copyProperties(mdmProcessRoutePage, mdmProcessRoute);
        mdmProcessRouteService.save(mdmProcessRoute);
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param mdmProcessRoutePage
     * @return
     */
    @AutoLog(value = "工艺路线-编辑")
    @ApiOperation(value = "工艺路线-编辑", notes = "工艺路线-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody MdmProcessRoutePage mdmProcessRoutePage) {
        MdmProcessRoute mdmProcessRoute = new MdmProcessRoute();
        BeanUtils.copyProperties(mdmProcessRoutePage, mdmProcessRoute);
        MdmProcessRoute mdmProcessRouteEntity = mdmProcessRouteService.getById(mdmProcessRoute.getId());
        if (mdmProcessRouteEntity == null) {
            return Result.error("未找到对应数据");
        }
        mdmProcessRouteService.updateById(mdmProcessRoute);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "工艺路线-通过id删除")
    @ApiOperation(value = "工艺路线-通过id删除", notes = "工艺路线-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        mdmProcessRouteService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "工艺路线-批量删除")
    @ApiOperation(value = "工艺路线-批量删除", notes = "工艺路线-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.mdmProcessRouteService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "工艺路线-通过id查询")
    @ApiOperation(value = "工艺路线-通过id查询", notes = "工艺路线-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MdmProcessRoute mdmProcessRoute = mdmProcessRouteService.getById(id);
        if (mdmProcessRoute == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mdmProcessRoute);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "工艺路线通过主表ID查询")
    @ApiOperation(value = "工艺路线主表ID查询", notes = "工艺路线-通主表ID查询")
    @GetMapping(value = "/queryProcessRouteDetailByMainId")
    public Result<?> queryProcessRouteDetailByMainId(@RequestParam(name = "id", required = true) String id) {
        List<MdmProcessRouteDetail> mdmProcessRouteDetailList = mdmProcessRouteDetailService.selectByMainId(id);
        return Result.OK(mdmProcessRouteDetailList);
    }

    /**
     * 分页列表查询-分类
     *
     * @param mdmProcessRouteClass
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "工艺路线-分类-分页列表查询")
    @ApiOperation(value = "工艺路线-分类-分页列表查询", notes = "工艺路线-分类-分页列表查询")
    @GetMapping(value = "/routeClassList")
    public Result<?> queryPageRouteClassList(MdmProcessRouteClass mdmProcessRouteClass,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<MdmProcessRouteClass> queryWrapper = QueryGenerator.initQueryWrapper(mdmProcessRouteClass, req.getParameterMap());
        Page<MdmProcessRouteClass> page = new Page<MdmProcessRouteClass>(pageNo, pageSize);
        IPage<MdmProcessRouteClass> pageList = mdmProcessRouteClassService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加-分类
     *
     * @param mdmProcessRouteClassPage
     * @return
     */
    @AutoLog(value = "工艺路线-分类-添加")
    @ApiOperation(value = "工艺路线-分类-添加", notes = "工艺路线-添加")
    @PostMapping(value = "/addRouteClass")
    public Result<?> addRouteClass(@RequestBody MdmProcessRouteClassPage mdmProcessRouteClassPage) {
        MdmProcessRouteClass mdmProcessRouteClass = new MdmProcessRouteClass();
        BeanUtils.copyProperties(mdmProcessRouteClassPage, mdmProcessRouteClass);
        mdmProcessRouteClassService.saveMain(mdmProcessRouteClass, mdmProcessRouteClassPage.getProcessRouteDetailList());
        return Result.OK("添加成功！");
    }

    /**
     * 编辑-分类
     *
     * @param mdmProcessRouteClassPage
     * @return
     */
    @AutoLog(value = "工艺路线-分类-编辑")
    @ApiOperation(value = "工艺路线-分类-编辑", notes = "工艺路线-分类-编辑")
    @PutMapping(value = "/editRouteClass")
    public Result<?> editRouteClass(@RequestBody MdmProcessRouteClassPage mdmProcessRouteClassPage) {
        MdmProcessRouteClass mdmProcessRouteClass = new MdmProcessRouteClass();
        BeanUtils.copyProperties(mdmProcessRouteClassPage, mdmProcessRouteClass);
        MdmProcessRouteClass mdmProcessRouteClassEntity = mdmProcessRouteClassService.getById(mdmProcessRouteClass.getId());
        if (mdmProcessRouteClassEntity == null) {
            return Result.error("未找到对应数据");
        }
        mdmProcessRouteClassService.updateMain(mdmProcessRouteClass, mdmProcessRouteClassPage.getProcessRouteDetailList());
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除-分类
     *
     * @param id
     * @return
     */
    @AutoLog(value = "工艺路线-分类-通过id删除")
    @ApiOperation(value = "工艺路线-分类-通过id删除", notes = "工艺路线-分类-通过id删除")
    @DeleteMapping(value = "/deleteRouteClass")
    public Result<?> deleteRouteClass(@RequestParam(name = "id", required = true) String id) {
        mdmProcessRouteClassService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除-分类
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "工艺路线-分类-批量删除")
    @ApiOperation(value = "工艺路线-分类-批量删除", notes = "工艺路线-分类-批量删除")
    @DeleteMapping(value = "/deleteBatchRouteClass")
    public Result<?> deleteBatchRouteClass(@RequestParam(name = "ids", required = true) String ids) {
        this.mdmProcessRouteClassService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param classId
     * @return
     */
    @AutoLog(value = "工艺路线通过分类ID查询")
    @ApiOperation(value = "工艺路线通过分类ID查询", notes = "工艺路线-通分类ID查询")
    @GetMapping(value = "/queryProcessRouteDetailByClassId")
    public Result<?> queryProcessRouteDetailByClassId(@RequestParam(name = "classId", required = true) String classId) {
        List<MdmProcessRouteDetail> mdmProcessRouteDetailList = mdmProcessRouteDetailService.selectByClassId(classId);
        return Result.OK(mdmProcessRouteDetailList);
    }

    /**
     * 生成分类编号
     *
     * @param
     * @return
     */
    @AutoLog(value = "工艺路线生成分类编号")
    @ApiOperation(value = "工艺路线生成分类编号", notes = "工艺路线生成分类编号")
    @GetMapping(value = "/generateClassNo")
    public Result<?> generateClassNo(@RequestParam(name = "mainId", required = true) String mainId, @RequestParam(name = "pid", required = false) String pid) {
        String classNo = mdmProcessRouteClassService.generateClassNo(mainId, pid);
        return Result.OK(classNo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mdmProcessRoute
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MdmProcessRoute mdmProcessRoute) {
        return super.exportXls(request, mdmProcessRoute, MdmProcessRoute.class, "工艺路线");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MdmProcessRoute.class);
    }

}
